<#include "common_var.include"/> 
package ${basepackage}.${moduleName}.service;

import ${basepackage}.${moduleName}.domain.${className};
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;

import java.util.List;
import java.util.Map;
/**
*
*${entityCnName}Dao
*
*/
@Repository
public class ${className}Dao{

	@Autowired
	private SpringJdbcTemplate springJdbc;

    //
	private static final String INSERT = "INSERT INTO ${tableClazz.underCodeName?upper_case} "
		+"(<#assign porpNo = 0><#list tableClazz.columns as prop><#if (porpNo>0)>,</#if>${prop.columnName?upper_case}<#assign porpNo=porpNo+1></#list>)"
		+"VALUES(<#assign porpNo = 0><#list tableClazz.columns as prop><#if (porpNo>0)>,</#if>?<#assign porpNo=porpNo+1></#list>)";


	private static final String UPDATE = "UPDATE ${tableClazz.underCodeName?upper_case} "
		+" SET <#assign porpNo = 0><#list tableClazz.columns as prop><#if !(prop.isPK())><#if (porpNo>0)>,</#if>${prop.columnName?upper_case}=?<#assign porpNo=porpNo+1></#if></#list>"
		+" WHERE ${tableClazz.pkProperty.columnName?upper_case} = ?";

    private static final String SELECT_BY_ID =
        "SELECT * FROM ${tableClazz.underCodeName} WHERE ${tableClazz.pkProperty.columnName} = ?";
    private static final String DELETE_BY_IDS =
        "DELETE FROM ${tableClazz.underCodeName} WHERE ${tableClazz.pkProperty.columnName} in (:idList)";


public int insert(${className} record){
        if(record==null)return 0;
        Object[] param = new Object[]{
        <#assign porpNo = 0><#list tableClazz.columns as prop><#if (porpNo>0)>,</#if>record.get${prop.name?cap_first}()<#assign porpNo=porpNo+1></#list>
        };
		int insertCount = springJdbc.getJdbcTemplate().update(INSERT,param);
		return insertCount;
    }

	public int[] batchInsert(Iterable<${className}> records){
        if(Iterables.isEmpty(records))return new int[]{};

        List<Object[]> paramList = Lists.newArrayList();
		for(${className} record:records) {
			Object[] param = new Object[]{
	<#assign porpNo = 0><#list tableClazz.columns as prop><#if (porpNo>0)>,</#if>record.get${prop.name?cap_first}()<#assign porpNo=porpNo+1></#list>
			};
			paramList.add(param);
		}

		int[] insertCount =
			springJdbc.getJdbcTemplate().batchUpdate(INSERT,paramList);
		return insertCount;
	}

    public int updateById(${className} record){
        if(record==null)return 0;
        Object[] param = new Object[]{
        <#list tableClazz.columns as prop><#if !(prop.isPK())>record.get${prop.name?cap_first}(),</#if></#list>
        record.get${tableClazz.pkProperty.name?cap_first}()};
		int updateCount = springJdbc.getJdbcTemplate().update(UPDATE,param);
		return updateCount;
	}

	public int[] batchUpdate(Iterable<${className}> records){
        if(Iterables.isEmpty(records))return new int[]{};

        List<Object[]> paramList = Lists.newArrayList();
		for(${className} record:records) {
			Object[] param = new Object[]{
		<#list tableClazz.columns as prop><#if !(prop.isPK())>record.get${prop.name?cap_first}(),</#if></#list>
			record.get${tableClazz.pkProperty.name?cap_first}()};
			paramList.add(param);
		}

		int[] updateCount =
			springJdbc.getJdbcTemplate().batchUpdate(UPDATE,paramList);
		return updateCount;
	}


    public int deleteByIds(List<String> ids){
        if(CollectionUtils.isEmpty(ids))return 0;
        Map<String,List> param = Maps.newHashMap();
		param.put("idList",ids);
		int delNum = springJdbc.getNamedParameterJdbcTemplate().update(DELETE_BY_IDS, param);
		return delNum;
    }

    public ${className} selectById(String id){
        return springJdbc.queryForBean(SELECT_BY_ID,new Object[]{id},${className}.class);
    }

}